//
//  HELMessageListVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/7.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELMessageListVC.h"
#import "HELMessageCell.h"
#import "HELMessageListModel.h"
#import "BTBaseWebVC.h"
@interface HELMessageListVC ()

@end

@implementation HELMessageListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
}

- (void)configUI{
  
    self.title = @"我的消息";
    
    [self.view addSubview:self.tableView];
    
     [self.tableView registerNib:[UINib nibWithNibName:@"HELMessageCell" bundle:nil] forCellReuseIdentifier:@"HELMessageCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 100;
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
         [weakSelf getMyMessageList:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
         [weakSelf getMyMessageList:NO];
    }];
    [self getMyMessageList:YES];
    

}
- (void)getMyMessageList:(BOOL)isRecover{
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
  
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:helNotificationList parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"][@"data"]isKindOfClass:[NSArray class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELMessageListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELMessageCell"];
    cell.model = self.dataArray[indexPath.row];
  
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    
    BTBaseWebVC *webVC = [[BTBaseWebVC alloc] init];
    HELMessageListModel *model =  self.dataArray[indexPath.row];
    NSString *url ;
    url = [NSString stringWithFormat:@"%@/%@/%@",[HELNetworkManager sharedMg].bathURL,kMessageDetail,model.pushId];
    webVC.theUrl = [NSURL URLWithString:url];
    //        webVC.title = model.title;
    [self.navigationController pushViewController:webVC animated:YES];
    if (![model.isRead integerValue]) {
        [self remakMessageRead:model];
    }
    
    
}


- (void)remakMessageRead:(HELMessageListModel *)model{
  
    
    weakSelf_define;
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",kMarkMessageRead,model.pushId] parameters:nil success:^(id responseObject) {
        if ([responseObject[@"data"] isKindOfClass:[NSString class]]) {
            if ([responseObject[@"data"] containsString:@"操作成功"]) {
                model.isRead = [@(1) stringValue];
            }
            [weakSelf.tableView reloadData];
        }
        
    } failure:^(NSString *err) {
        
    }];
    

}






@end
